<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<!--引入easyui的样式 -->
<link rel="stylesheet"  type="text/css" href="${pageContext.request.contextPath}/themes/bootstrap/easyui.css">
<link rel="stylesheet"  type="text/css" href="${pageContext.request.contextPath}/themes/icon.css">
<!-- js文件 -->
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/easyui-lang-zh_CN.js"></script>
</head>
<script type="text/javascript">
$(function(){
	<!--更改底部工具栏 -->
	var pager=$("#UserList").datagrid().datagrid('getPager');
	pager.pagination({
		pageList:[20,30,50],
	});
});
//显示增加
function addInfo(){
 $('#dlg').dialog('open').dialog('center').dialog('setTitle','增加');
 $('#fm').form('clear');
 $('#submit').hide();
 $('#save').show();
}
function save(){
	  	var user=new Object();
		user.uacount = $("#uacount").val();
		user.upassword = $("#upassword").val();
		user.uname = $("#uname").val();
		user.rid=$('#cc').combobox('getValue');
		var json=JSON.stringify(user);
  		$.ajax({
	  		type:"post",
			url:"${pageContext.request.contextPath}/user/addUser.action",
			data:{"jsonadd":json},
			dataType:"text",
			success:function(data){ 
				if(data=="true"){
					$.messager.alert('提示','添加成功','info',function(){
						$('#dg').datagrid('reload');
						$('#dlg').dialog('close');
					})
				}
		 }   		   		
  	});
}
//删除
function deleteInfo() {
	var data = $("#dg").datagrid('getSelections');	
	if(data.length==0){
		$.messager.show({
			title:'警告',
			msg:'请选中需要删除的数据',
			timeout:1500,
			showType:'slide',
				style:{
					left:100,
					right:'',
					top:100
				}
	  		});
		}else{
			//2.声明一个空的数组	装选中的ID
			var uids=[];
			for (var  i= 0;  i< data.length; i++) {
				uids.push(data[i].uid)			
			}
			var params = $.param({'uids':uids},true);
			$.messager.confirm('确认删除', '您确定要删除这'+data.length+'条数据吗？', function(r){				
				    $.ajax({
				    	type:"post",
				    	url:"${pageContext.request.contextPath}/user/deleteUser.action",
				    	data:params,
				    	dataType:"text",
				    	success:function(data){	
				    		$.messager.alert('提示', data)
				    		$("#dg").datagrid('reload');				    			  				    							    		
				    	}
				    })					
		});
	}
}
//修改
function updateUser(){
	 var user =$("#dg").datagrid('getSelected');
	 var data = $("#dg").datagrid('getSelections');	
		if(data.length==0){
			$.messager.show({
				title:'警告',
				msg:'请选中需要修改的数据',
				timeout:1500,
				showType:'slide',
					style:{
						left:100,
						right:'',
						top:100
					}
		  		});
		}else if(data.length!=1){
			$.messager.show({
				title:'警告',
				msg:'你只能选择一条数据修改',
				timeout:1500,
				showType:'slide',
					style:{
						left:100,
						right:'',
						top:100
					}
		  		});
		}else{
			$("#uacount").textbox('setValue',user.uacount)
			$("#upassword").textbox('setValue',user.upassword);
			$("#uname").textbox('setValue',user.uname);
			
			
			  $('#dlg').dialog('open').dialog('center').dialog('setTitle','修改信息');
			  //隐藏保存按钮
				$('#save').hide();
			    //显示确认按钮
			     $('#submit').show();
		}
		
}		    
function submit(){
			var user=new Object();
			user.uid= $("#uid").val();
			user.uacount = $("#uacount").val();
			user.upassword = $("#upassword").val();
			user.uname = $("#uname").val();
			user.rid=$('#cc').combobox('getValue');
			var json=JSON.stringify(user);   	
		$.ajax({	   				
				type:"post",
				url:"${pageContext.request.contextPath}/user/updateUser.action",
				data:{"jsont":json},
				dataType:"text",
				success:function(data){
					$.messager.alert('提示', data)
					$('#dg').datagrid('reload');  
				 $('#dlg').dialog('close')
				}
		})
}
</script>
<body>
 <table id="dg" title="用户列表" class="easyui-datagrid" style="width:1000px;height:500px" pageSize="10"
            url="${pageContext.request.contextPath}/user/showUserMsg.action" toolbar="#toolbar" pagination="true" rownumbers="true" fitColumns="true">
        <thead>
            <tr>
            	<th field="ck" checkbox=true></th>
                <th field="uid" width="50">用户id</th>
                <th field="uacount" width="50">账户名</th>
                <th field="upassword" width="50">密码</th>
                <th field="uname" width="50">用户名</th>
                <th field="rid" width="50" id="rid">角色id</th>
                <th field="rname" width="50">角色名</th>
            </tr>
        </thead>
         <div id="toolbar">
        		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" id="add" onclick="addInfo()">增加</a>
        		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" id="updateUser" onclick="updateUser()">修改</a>
        		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" id="delete" onclick="deleteInfo()">删除</a>
   		 </div>
   		 
   		 <div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
            closed="true" buttons="#dlg-buttons">
        <div class="ftitle" id="">用户信息</div>
        <form id="fm" method="post">
            <div class="fitem">
                <label>账户名:</label>
                <input id="uacount" name="uacount" class="easyui-textbox" required="true">
            </div>
            <div class="fitem">
                <label>密码:</label>
                <input id="upassword" name="upassword" class="easyui-textbox" required="true">
            </div>
            <div class="fitem">
                <label>用户名:</label>
                <input id="uname" name="uname" class="easyui-textbox" required="true">
            </div>
            <div class="fitem">
               <input id="cc" class="easyui-combobox" name="dept"   
    			data-options="valueField:'id',textField:'text',url:'${pageContext.request.contextPath}/user/getRname.action'" />  
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
    	<a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" style="width:90px;" id="submit" onclick="submit()">提交</a>
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" style="width:90px;" id="save" onclick="save()">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
    </div>
    </table>
</body>
</html>